import random
import pygame

#定义常量全部大写
SCREEN_INFO = pygame.Rect(0,0,450,700)
FRAME_PRE_SEC = 60
CREATE_ENEMY_EVANT = pygame.USEREVENT
MAN_BULLET_EVENT = pygame.USEREVENT+1
class GameScripte(pygame.sprite.Sprite) :
    def __init__(self,iamge_name,speed=1):
        super().__init__()
        self.image = pygame.image.load(iamge_name)
        self.rect =  self.image.get_rect()
        self.speed = speed

    def update(self):
        self.rect.y += self.speed


class Backgroud(GameScripte):
    def __init__(self,second=False):
        super().__init__('./images/bj.jpg')
        if second :
            self.rect.y = -self.rect.height

    def update(self):
        super().update()
        if self.rect.y > SCREEN_INFO.height :
            self.rect.y = -self.rect.height

class SuperMan(GameScripte):
    def __init__(self,):
        super().__init__("./images/user.png",0)
        self.rect.centerx = SCREEN_INFO.centerx
        self.rect.bottom = SCREEN_INFO.bottom -120
        self.bullet_group = pygame.sprite.Group()

    def update(self,key_pressed=False) :
        if key_pressed :
            if key_pressed == 1 and self.rect.x < SCREEN_INFO.width - self.rect.width :
                self.rect.x += 2
            elif key_pressed == 2 and self.rect.x >0:
                self.rect.x -=2
        else :
            x, y = pygame.mouse.get_pos()
            if self.rect.width / 2 <= x <= SCREEN_INFO.width - self.rect.width / 2:
                self.rect.x = x - self.rect.height / 2

    def launch(self):

        #2.设置精灵的位置
        for i in (0,1,2) :
            # 1.创建子弹精灵
            bullet = Bullet()
            bullet.rect.bottom = self.rect.y - i * 50
            bullet.rect.centerx = self.rect.centerx
            #3,将精灵添加精灵组
            self.bullet_group.add(bullet)

class Enemy(GameScripte) :
    def __init__(self):
            super().__init__('./images/hat.png')
            self.speed = random.randint(1,3)
            self.rect.x = random.randint(1,SCREEN_INFO.width-self.rect.width)
    def update(self):
            super(Enemy, self).update()
            if self.rect.y >= SCREEN_INFO.height :
                print('飞出')
                self.kill()

class  Bullet(GameScripte) :
    def __init__(self):
        super().__init__("./images/q-1.png",-2)
    def update(self):
        super().update()
        if self.rect.bottom < 0 :
            self.kill()
    def __del__(self):
        print('子弹被销毁')
